package GUI;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import Streamrecipient.RTPClient;

public class MainFrame implements ActionListener{

	
	public static void run() {
		
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				new MainFrame();
			}
		});
	}
	
	private String serverIP;
	private RTPClient client;
	private JLabel label;
	private String connection;
	
	private MainFrame(){
		initUI();
		client = new RTPClient();
	}

	private void initUI() {
		final JFrame frame = new JFrame("It was Pain! Client");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(400, 100);
		
		//mainpanel
		JPanel mainpanel = new JPanel(new BorderLayout());
		mainpanel.setBorder(BorderFactory.createTitledBorder("Info:"));
		connection = "Not connected";
		label = new JLabel(connection);
		mainpanel.add(label, BorderLayout.NORTH);
		
		
		//Menu(File/Exit)
		JMenuBar bar = new JMenuBar();
		JMenu fileMenu = new JMenu("File");
		
		JMenuItem exit = new JMenuItem("Exit");
		exit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				frame.dispose();
			}
		});
		
		
		// Menu (Stream/)
		
		JMenu streamMenu = new JMenu("Stream");		
		JMenuItem connect = new JMenuItem("Connect to Server");
		streamMenu.add(connect);
		connect.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				serverIP = JOptionPane.showInputDialog("Please enter the IP of the server:");
				client.listenToStream(serverIP);
				update();
			}

		});
		
		
		fileMenu.add(exit);
		bar.add(fileMenu);
		bar.add(streamMenu);
		frame.setJMenuBar(bar);
		frame.setContentPane(mainpanel);
		frame.setResizable(false);
		frame.setVisible(true);
	
	}
	
	private void update(){
		connection = serverIP;
		label.repaint();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		label.setText(e.getActionCommand());
		label.updateUI();
	}
}